﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.MobileServices;
using System.IO.IsolatedStorage;

namespace mobileS.GoodsList
{
    public class GoodSet
    {
        [JsonProperty(PropertyName = "id")]
        public int Id { get; set; }

        [JsonProperty(PropertyName = "Name")]
        public string Name { get; set; }

        [JsonProperty(PropertyName = "Latitude")]
        public decimal Latitude { get; set; }

        [JsonProperty(PropertyName = "Longitude")]
        public decimal Longitude { get; set; }

        [JsonProperty(PropertyName = "SiteId")]
        public decimal SiteId { get; set; }
    }

    public partial class GoodsList : PhoneApplicationPage
    {
        public GoodsList()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            try
            {
                int siteId = int.Parse(NavigationContext.QueryString["siteId"]);
                LoadGoods(siteId);
            }
            catch
            {
                NavigationService.GoBack();
            }
        }

        private async void LoadGoods(int siteId)
        {
            ListItems.ItemsSource = null;
            loadingText.Visibility = Visibility.Visible;
            loagindBar.Visibility = Visibility.Visible;
            IMobileServiceTable<GoodSet> goodsSetTable = App.MobileService.GetTable<GoodSet>();
            if (IsolatedStorageSettings.ApplicationSettings.Contains("LocationLatitude") && (IsolatedStorageSettings.ApplicationSettings["LocationLatitude"] != null)
                && IsolatedStorageSettings.ApplicationSettings.Contains("LocationLongitude") && (IsolatedStorageSettings.ApplicationSettings["LocationLongitude"] != null))
            {
                ListItems.ItemsSource = await goodsSetTable.Where(goodItem => (goodItem.SiteId == siteId) && ((Decimal.Parse((string)IsolatedStorageSettings.ApplicationSettings["LocationLatitude"])) - 4 < goodItem.Latitude) && ((Decimal.Parse((string)IsolatedStorageSettings.ApplicationSettings["LocationLatitude"])) + 4 > goodItem.Latitude) && ((Decimal.Parse((string)IsolatedStorageSettings.ApplicationSettings["LocationLongitude"])) - 4 < goodItem.Longitude) && ((Decimal.Parse((string)IsolatedStorageSettings.ApplicationSettings["LocationLongitude"])) + 4 > goodItem.Longitude)).ToCollectionAsync();
                if (ListItems.ItemsSource.Count > 0)
                {
                    GoodsLocation.Text = "Goods close to you...";
                    DesertImg.Visibility = Visibility.Collapsed;
                }
                else
                {
                    GoodsLocation.Text = "We have no goods close to you.";
                    DesertImg.Visibility = Visibility.Visible;
                }
            }
            else
            {
                ListItems.ItemsSource = await goodsSetTable.Where(goodItem => (goodItem.SiteId == siteId)).ToCollectionAsync();
                if (ListItems.ItemsSource.Count > 0)
                {
                    GoodsLocation.Text = "All the goods...";
                }
                else
                {
                    GoodsLocation.Text = "We have no goods in this site.";
                    DesertImg.Visibility = Visibility.Visible;
                }
            }
            loadingText.Visibility = Visibility.Collapsed;
            loagindBar.Visibility = Visibility.Collapsed;
        }

        private void NavigateToGoodsList(object sender, RoutedEventArgs e)
        {
            int goodId = ((GoodSet)((Button)sender).DataContext).Id;
            NavigationService.Navigate(new Uri("/GoodDetails.xaml?goodId=" + goodId, UriKind.Relative));
        }
    }
}